package cn.com.frame.app.service;

import cn.com.frame.app.mapper.FtMenuMapper;
import cn.com.frame.app.pojo.po.FtMenu;
import cn.com.frame.commom.PageBasic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

@Service
public class FtMenuService {

    @Autowired
    private FtMenuMapper ftMenuMapper;

    @Autowired
    private TransactionTemplate transactionTemplate;

    public PageBasic<FtMenu> getPageData(Integer page, Integer size , String menuName){
        PageBasic<FtMenu>  pageBasic= new PageBasic<>(page,size);
        pageBasic.setRows(ftMenuMapper.getList(menuName));
        return pageBasic;
    }

    public FtMenu selectByPrimaryKey(Long id){
        return ftMenuMapper.selectByPrimaryKey(id);
    }

    public void updateByPrimaryKey(FtMenu ftMenu){
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                FtMenu ftMenuOld = ftMenuMapper.selectByPrimaryKey(ftMenu.getMenuId());
                ftMenuOld.setMenuName(ftMenu.getMenuName());
                ftMenuMapper.updateByPrimaryKey(ftMenuOld);
            }
        });

    }

    public int insert(FtMenu ftMenu){
         return ftMenuMapper.insertSelective(ftMenu);
    }

    public int delete(Long id){
        return ftMenuMapper.deleteByPrimaryKey(id);
    }



}
